package JassTranslator;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import framework.Translator;
import framework.TranslatorUtils;

import MainPackage.TranslateFile;

public class ImportJass extends Translator {
	List<JassLineTranslator> translators = new ArrayList<JassLineTranslator>();

	public ImportJass() {
		super();
	}

	public void initTranslators() {
		translators.add(new TranslateFunction());
	}

	public String getExportFileExtension() {
		return ".java";
	}

	public String getImportFileExtension() {
		return ".j";
	}

	public static class TranslateFunction extends JassLineTranslator {
		public String basicTranslator(String codeString) {
			codeString = super.basicTranslator(codeString);
			codeString = codeString.replace(" takes void", "(");
			codeString = codeString.replace(" takes ", "(");
			codeString = codeString.replace(" returns", ")");
			return codeString;
		}

		public boolean isLineOfType(String s) {
			return (StringUtils.contains(s, "function ")
					&& !StringUtils.contains(s, "endfunction ")
					|| StringUtils.contains(s, "method ")
					&& !StringUtils.contains(s, "endmethod "))
					&& !StringUtils.startsWith(s, "native");
		}

		public String mainLineTranslator(String codeString) {
			String[] codeWords = StringUtils.split(codeString.trim(), " ");

			String returnType = codeWords[codeWords.length - 1];
			codeString = StringUtils.replace(codeString, returnType, "{");
			codeString = StringUtils.replaceOnce(codeString, "function",
					returnType);
			codeString = StringUtils.replaceOnce(codeString, "method",
					returnType);
			codeString = checkForAndAddAccessModifier(codeString);

			return codeString;
		}
	}

	public static class TranslateEndFunction extends JassLineTranslator {

		public boolean isLineOfType(String s) {
			return StringUtils.startsWith(s, "endfunction")
					|| StringUtils.startsWith(s, "endmethod");
		}

		public String mainLineTranslator(String codeString) {
			codeString = StringUtils.replace(codeString, "endfunction", "}");
			codeString = StringUtils.replace(codeString, "endmethod", "}");
			return codeString;
		}

	}

	public static class TranslateCall extends JassLineTranslator {

		public String basicTranslator(String codeString) {
			codeString = super.basicTranslator(codeString);
			codeString = codeString.replace("call ", "");
			return codeString;
		}

		public boolean isLineOfType(String s) {
			return StringUtils.startsWith(s, "call ");
		}

		public String mainLineTranslator(String codeString) {
			return codeString + ";";
		}

	}

	public static class TranslateSet extends JassLineTranslator {
		public String basicTranslator(String codeString) {
			codeString = super.basicTranslator(codeString);
			codeString = codeString.replace("set ", "");
			return codeString;
		}

		public boolean isLineOfType(String s) {
			return StringUtils.startsWith(s, "set ");
		}

		public String mainLineTranslator(String codeString) {
			return codeString + ";";
		}
	}

	public static class TranslateReturn extends JassLineTranslator {
		public String basicTranslator(String codeString) {
			codeString = super.basicTranslator(codeString);
			return codeString;
		}

		public boolean isLineOfType(String s) {
			return StringUtils.startsWith(s, "return ");
		}

		public String mainLineTranslator(String codeString) {
			return codeString + ";";
		}
	}

	public static class TranslateLoop extends JassLineTranslator {

		public boolean isLineOfType(String codeString) {
			if (codeString.equals("loop") || codeString.equals("endloop")) {
				return true;
			}
			return false;
		}

		public String mainLineTranslator(String codeString) {
			if (StringUtils.equals(codeString, "loop")) {
				return "while(true) {";
			} else if (StringUtils.equals(codeString, "endloop")) {
				return "}";
			}
			return "}";
		}
	}

	public static class TranslateExitWhen extends JassLineTranslator {
		public boolean isLineOfType(String codeString) {
			if (StringUtils.startsWith(codeString, "exitwhen")) {
				return true;
			}
			return false;
		}

		public String mainLineTranslator(String codeString) {
			codeString = StringUtils
					.replaceOnce(codeString, "exitwhen", "if (");
			codeString = codeString + ") break;";
			return codeString;
		}
	}

	public static class TranslateClass extends JassLineTranslator {
		public boolean isLineOfType(String codeString) {
			if (StringUtils.contains(codeString, " class ")
					|| StringUtils.startsWith(codeString, "endclass")) {
				return true;
			}
			return false;
		}

		public String mainLineTranslator(String codeString) {
			if (StringUtils.contains(codeString, " class ")) {
				return codeString + " {";
			} else {
				return StringUtils.replace(codeString, "endclass", "}");
			}
		}
	}
	
	public static class TranslateLocal extends JassLineTranslator {
		public boolean isLineOfType(String codeString) {
			if (StringUtils.startsWith(codeString, "local")) {
			    return true;
			}
			return false;
		}

		public String mainLineTranslator(String codeString) {
			return StringUtils.replace(codeString, "local ", "") + ";";
		}
	}
	
	public static class TranslateNative extends JassLineTranslator {
		public boolean isLineOfType(String codeString) {
			if (StringUtils.startsWith(codeString, "native")) {
			    return true;
			}
			return false;
		}

		public String mainLineTranslator(String codeString) {
			return codeString;
		}
		
		public List<String> translateLineList(String codeString){
			List<String> returnList = new ArrayList<String>(3);
			codeString = StringUtils.replace(codeString, "native ", "");
			String indent = TranslatorUtils.getIndentationString(codeString);
			returnList.add(new TranslateFunction().translateLine(codeString));
			returnList.add(indent + "//Add coding here");
			returnList.add(indent + "}");
			return returnList;
			
		}
	}

}
